package org.tldb.demo.lock;

import io.github.donnie4w.tlmq.cli.MqClient;
import io.github.donnie4w.tlmq.cli.SimpleClient;
import io.github.donnie4w.tlmq.cli.TlException;

/**
 * 测试分布式锁：lock
 */
public class TestLock {

    public static void main(String[] args) throws TlException {
        MqClient mc = new SimpleClient("ws://127.0.0.1:5000", "mymq=123");
        mc.connect();

        String key = null;
        try {
            key = mc.lock("testlock", 3); //获取分布式锁
            // 执行业务逻辑程序
            System.out.println("获取分布式成功，执行业务逻辑程序...");
        } finally {
            // 释放分布式锁
            if (key != null) {
                mc.unLock(key);
                System.out.println("释放分布式锁成功");
            }
        }
    }
}
